package com.frontop.system.config;

import com.frontop.system.modules.sale.model.SaleInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * @author YangBoss
 * @title: MultipartFlieUtil
 * @projectName frontop-ao
 * @description: TODO
 * @date 2022/6/16 18:37
 */
public class MultipartFlieUtil {

    /**
     * 上传文件
     * @param multipartFile
     * @param path
     * @return
     */
    public static String upFile(MultipartFile multipartFile,String path) {
        //获取文件后缀
        String fileType = multipartFile.getOriginalFilename().substring(multipartFile.getOriginalFilename().lastIndexOf("."));

        try {
            String uuid = String.valueOf(UUID.randomUUID());
            String uploadPath = path+"/"+uuid+fileType;
            File file = new File(path);
            if(!file.exists()){
                file.mkdirs();
            }
            multipartFile.transferTo(new File(uploadPath));

            return uuid+fileType;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }
}
